<?php

class model_content_article extends model
{

    public function get_articles($category_id = -1, $offset = 0, $limit = 20)
    {
        
        $query = 'SELECT * FROM `bone_content_article` WHERE `block`=0';
        if ($category_id == 0)
            $query .= ' AND `category_id`=0';
        else if ($category_id > 0)
        {
            $model = bone::get_model('content', 'category');
            $ids = $model->get_sub_category_ids($category_id);
            if (count($ids) > 0)
            {
                $ids[] = $category_id;
                $query .= ' AND `category_id` IN(' . implode(',', $ids) . ')';
            }
            else
                $query .= ' AND `category_id`=' . $category_id;
        }
        $query .= ' ORDER BY `ordering` ASC';
        
        $db = bone::get_db();
        return $db->get_objs($query, $offset, $limit);
    
    }

    public function get_article_sum($category_id = -1)
    {
        $query = 'SELECT * FROM `bone_content_article` WHERE `block`=0';
        if ($category_id == 0)
            $query .= ' AND `category_id`=0';
        else if ($category_id > 0)
        {
            $model = bone::get_model('content', 'category');
            $ids = $model->get_sub_category_ids($category_id);
            if (count($ids) > 0)
            {
                $ids[] = $category_id;
                $query .= ' AND `category_id` IN(' . implode(',', $ids) . ')';
            }
            else
                $query .= ' AND `category_id`=' . $category_id;
        }
        
        $db = bone::get_db();
        return $db->get_result($query);
    }

}
?>